package com.ruicar.afs.cloud.apply.pre.loan.mq.receiver;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormClass;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormFieldMapping;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormManage;
import com.ruicar.afs.cloud.bizcommon.print.entity.ComPrintFormSealMapping;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormClassService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormFieldMappingService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormManageService;
import com.ruicar.afs.cloud.bizcommon.print.service.ComPrintFormSealMappingService;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateDataSendVo;
import com.ruicar.afs.cloud.bizcommon.print.vo.TemplateRuleVo;
import com.ruicar.afs.cloud.bizcommon.rules.entity.*;
import com.ruicar.afs.cloud.bizcommon.rules.mapper.*;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleOperateHisService;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 合同模板MQ接收处理
 *
 * @author 耿继辉
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyComPrintFormDealProcessor implements AfsMqBizProcessor<TemplateDataSendVo> {
    /**
     * 打印模板service(com_print_form_class)
     */
    private final ComPrintFormClassService comPrintFormClassService;
    /**
     * 影像service (com_attachement_file)
     */
    private final ComAttachmentFileService comAttachmentFileService;
    /**
     * 打印模板管理service(com_print_form_manage)
     */
    private final ComPrintFormManageService comPrintFormManageService;
    /**
     * 打印模板字段service(com_print_form_field_mapping)
     */
    private final ComPrintFormFieldMappingService comPrintFormFieldMappingService;
    /**
     * 规则
     */
    private final AfsRuleInfoService ruleInfoService;
    private final AfsRuleInputMappingMapper mappingMapper;
    private final AfsRuleItemMapper ruleItemMapper;
    private final AfsRuleOperateHisService ruleOperateHisService;
    private final AfsRuleTransExpressMapper ruleTransExpressMapper;
    private final AfsRuleExpressMapper ruleExpressMapper;
    private final AfsRuleExpressAtomMapper ruleExpressAtomMapper;
    private final AfsRuleResultMappingMapper afsRuleResultMappingMapper;
    private final ComPrintFormSealMappingService comPrintFormSealMappingService;

    @Override
    public boolean processMessage(TemplateDataSendVo entity) throws Exception {
        //接收到含有数据得报文
        if (ObjectUtil.isNotNull(entity)) {
            //解析报文
            this.messageDeal(entity);
        } else {
            log.error("合同模板报文为空,请确定是否传值");
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_NEW_TEMPLATE;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }

    /**
     * 解析报文
     *
     * @param vo
     */
    public void messageDeal(TemplateDataSendVo vo) {
        List<ComPrintFormClass> classList = vo.getClassList();
        ComAttachmentFile comAttachmentFile = vo.getComAttachmentFile();
        ComPrintFormManage comPrintFormManage = vo.getComPrintFormManage();
        List<ComPrintFormFieldMapping> fieldList = vo.getFieldList();
        List<TemplateRuleVo> ruleVoList = vo.getRuleVoList();
        List<ComPrintFormSealMapping> comPrintFormSealMappingList = vo.getComPrintFormSealMappingList();
        //印章和签名类别
        if (CollectionUtil.isNotEmpty(comPrintFormSealMappingList)) {
            for (ComPrintFormSealMapping comPrintFormSealMapping : comPrintFormSealMappingList) {
                //先查
                ComPrintFormSealMapping byId = this.comPrintFormSealMappingService.getById(comPrintFormSealMapping);
                if (ObjectUtil.isNotNull(byId)) {
                    this.comPrintFormSealMappingService.updateById(comPrintFormSealMapping);
                } else {
                    this.comPrintFormSealMappingService.save(comPrintFormSealMapping);
                }
            }
        }
        //打印模板类别
        if (CollectionUtil.isNotEmpty(classList)) {
            for (ComPrintFormClass comPrintFormClass : classList) {
                //先查
                ComPrintFormClass byId = this.comPrintFormClassService.getById(comPrintFormClass);
                if (ObjectUtil.isNotNull(byId)) {
                    this.comPrintFormClassService.updateById(comPrintFormClass);
                } else {
                    this.comPrintFormClassService.saveTemplateClass(comPrintFormClass);
                }
            }
            log.info("报文解析完成，数据入库");
        }
        //影像件
        if (ObjectUtil.isNotNull(comAttachmentFile)) {
            ComAttachmentFile byId = this.comAttachmentFileService.getById(comAttachmentFile);
            if (ObjectUtil.isNotNull(byId)) {
                this.comAttachmentFileService.updateById(comAttachmentFile);
            } else {
                this.comAttachmentFileService.save(comAttachmentFile);
            }
            log.info("报文解析完成，数据入库");
        }
        //打印模板管理
        if (ObjectUtil.isNotNull(comPrintFormManage)) {
            ComPrintFormManage byId = this.comPrintFormManageService.getById(comPrintFormManage);
            if (ObjectUtil.isNotNull(byId)) {
                this.comPrintFormManageService.updateById(comPrintFormManage);
            } else {
                this.comPrintFormManageService.saveTemplate(comPrintFormManage);
            }
            log.info("报文解析完成，数据入库");
        }
        //打印模板字段
        if (CollectionUtil.isNotEmpty(fieldList)) {
            for (ComPrintFormFieldMapping comPrintFormFieldMapping : fieldList) {
                ComPrintFormFieldMapping byId = this.comPrintFormFieldMappingService.getById(comPrintFormFieldMapping);
                if (ObjectUtil.isNotNull(byId)) {
                    this.comPrintFormFieldMappingService.updateById(comPrintFormFieldMapping);
                } else {
                    this.comPrintFormFieldMappingService.saveFieldMapping(comPrintFormFieldMapping);
                }
            }
            log.info("报文解析完成，数据入库");
        }
        //规则
        if (CollectionUtil.isNotEmpty(ruleVoList)) {
            for (TemplateRuleVo templateRuleVo : ruleVoList) {
                List<AfsRuleExpress> afsRuleExpressList = templateRuleVo.getRuleExpress();
                List<AfsRuleExpressAtom> afsRuleExpressAtomList = templateRuleVo.getRuleExpressAtom();
                AfsRuleInfo ruleInfo = templateRuleVo.getRuleInfo();
                List<AfsRuleInputMapping> afsRuleInputMappingList = templateRuleVo.getRuleInputMapping();
                List<AfsRuleItem> afsRuleItemList = templateRuleVo.getRuleItem();
                List<AfsRuleOperateHis> ruleOperateHisList = templateRuleVo.getRuleOperateHis();
                List<AfsRuleResultMapping> afsRuleResultMappingList = templateRuleVo.getRuleResultMapping();
                List<AfsRuleTransExpress> transExpressList = templateRuleVo.getRuleTransExpress();
                if (CollectionUtil.isNotEmpty(afsRuleExpressList)) {
                    for (AfsRuleExpress afsRuleExpress : afsRuleExpressList) {
                        //先根据id查询，如果能查到数据，那么就更新
                        AfsRuleExpress ruleExpress = this.ruleExpressMapper.selectById(afsRuleExpress.getId());
                        if (ObjectUtil.isNotNull(ruleExpress)) {
                            this.ruleExpressMapper.updateById(afsRuleExpress);
                        } else {
                            //如果库内没有数据，那么就直接插入
                            this.ruleExpressMapper.insert(afsRuleExpress);
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(afsRuleExpressAtomList)) {
                    for (AfsRuleExpressAtom afsRuleExpressAtom : afsRuleExpressAtomList) {
                        AfsRuleExpressAtom ruleExpressAtom = this.ruleExpressAtomMapper.selectById(afsRuleExpressAtom.getId());
                        if (ObjectUtil.isNotNull(ruleExpressAtom)) {
                            this.ruleExpressAtomMapper.updateById(afsRuleExpressAtom);
                        } else {
                            this.ruleExpressAtomMapper.insert(afsRuleExpressAtom);
                        }
                    }
                }
                if (ObjectUtil.isNotNull(ruleInfo)) {
                    AfsRuleInfo byId = this.ruleInfoService.getById(ruleInfo);
                    if (ObjectUtil.isNotNull(byId)) {
                        this.ruleInfoService.updateById(ruleInfo);
                    } else {
                        this.ruleInfoService.save(ruleInfo);
                    }
                }
                if (CollectionUtil.isNotEmpty(afsRuleInputMappingList)) {
                    for (AfsRuleInputMapping afsRuleInputMapping : afsRuleInputMappingList) {
                        AfsRuleInputMapping ruleInputMapping = this.mappingMapper.selectById(afsRuleInputMapping);
                        if (ObjectUtil.isNotNull(ruleInputMapping)) {
                            this.mappingMapper.updateById(afsRuleInputMapping);
                        } else {
                            this.mappingMapper.insert(afsRuleInputMapping);
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(afsRuleItemList)) {
                    for (AfsRuleItem afsRuleItem : afsRuleItemList) {
                        AfsRuleItem ruleItem = this.ruleItemMapper.selectById(afsRuleItem);
                        if (ObjectUtil.isNotNull(ruleItem)) {
                            this.ruleItemMapper.updateById(afsRuleItem);
                        } else {
                            this.ruleItemMapper.updateById(afsRuleItem);
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(ruleOperateHisList)) {
                    for (AfsRuleOperateHis afsRuleOperateHis : ruleOperateHisList) {
                        AfsRuleOperateHis byId = this.ruleOperateHisService.getById(afsRuleOperateHis.getId());
                        if (ObjectUtil.isNotNull(byId)) {
                            this.ruleOperateHisService.updateById(afsRuleOperateHis);
                        } else {
                            this.ruleOperateHisService.save(afsRuleOperateHis);
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(afsRuleResultMappingList)) {
                    for (AfsRuleResultMapping afsRuleResultMapping : afsRuleResultMappingList) {
                        AfsRuleResultMapping ruleResultMapping = this.afsRuleResultMappingMapper.selectById(afsRuleResultMapping);
                        if (ObjectUtil.isNotNull(ruleResultMapping)) {
                            this.afsRuleResultMappingMapper.updateById(afsRuleResultMapping);
                        } else {
                            this.afsRuleResultMappingMapper.insert(afsRuleResultMapping);
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(transExpressList)) {
                    for (AfsRuleTransExpress afsRuleTransExpress : transExpressList) {
                        AfsRuleTransExpress ruleTransExpress = this.ruleTransExpressMapper.selectById(afsRuleTransExpress);
                        if (ObjectUtil.isNotNull(ruleTransExpress)) {
                            this.ruleTransExpressMapper.updateById(afsRuleTransExpress);
                        } else {
                            this.ruleTransExpressMapper.insert(afsRuleTransExpress);
                        }
                    }
                }
                log.info("报文解析完成，数据入库");
            }
        }
    }
}
